#include "ms_hal_ble.h"
#include "stdio.h"
#include <stdlib.h>

#define MS_LOGD(FMT, ...) printf("\e[0;34m%s:%d:" FMT "\e[0m\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
TEST_ASSERT_EQUAL_UINT8(int x, int y)
{
    if (x != y)
        printf("\e[0;31m%s:%d test error %d %d\e[0m\n", __FUNCTION__, __LINE__, x, y);
    else
        printf("%s:%d test success %d %d\n", __FUNCTION__, __LINE__, x, y);
    ;
}
static void mst_hal_ble_scan_time_process(void) { sleep(5); }
char *aaaaddr = "11:F9:E9:EE:42:81";

#define MS_BLE_DISC_SVC_TIMEOUT (5 * 1000)
#define MS_BLE_CONN_INTERVAL_MIN 0x0028  // 0x28 = 40 * 1.25ms = 50ms (6~3200 means 7.5ms~4S)
#define MS_BLE_CONN_INTERVAL_MAX 0x0028  // 0x28 = 40 * 1.25ms = 50ms (6~3200 means 7.5ms~4S)
#define MS_BLE_CONN_LETENCY 0x00         // 0
#define MS_BLE_SUPERVISION_TIMEOUT 0x1F4 // 0x1F4 = 500 * 10ms (Supervision_Timeout 10~3200 means 100ms~32S)
#define MS_BLE_DEFAULT_MTU 250
#define MS_BLE_MAC_LEN 6

typedef union {
    struct
    {
        uint16_t at_cmd : 1;
        uint16_t conn : 1;
        uint16_t disc_service : 1;
        uint16_t disc_service_cmp : 1;
        uint16_t disc_char : 1;
        uint16_t disc_char_cmp : 1;
        uint16_t disc_desc : 1;
        uint16_t disc_desc_cmp : 1;
        uint16_t mtu_cmp : 1;
        uint16_t conn_param_update : 1;
        uint16_t write_req : 1;
        uint16_t indicate_req : 1;
        uint16_t res : 4;
    } bits;
    uint16_t flags;
} mst_ble_central_flag_t;

typedef struct
{
    uint16_t conn_hdl;
    // start and end of the ff10 handle
    uint16_t ff10_start_hdl;
    uint16_t ff10_end_hdl;
    // ff10 service characteristic handle
    uint16_t ff11_hdl;
    uint16_t ff12_hdl;

    uint16_t ff12_cccd_hdl;

    //  start and end of the ff20 handle
    uint16_t ff20_start_hdl;
    uint16_t ff20_end_hdl;
    // ff20 service characteristic handle
    uint16_t ff21_hdl;
    uint16_t ff22_hdl;

    uint16_t ff22_cccd_hdl;
} mst_ble_central_t;

struct mst_hal_ble_mac
{
    uint8_t type;
    uint8_t addr[6];
};

static mst_ble_central_t g_service = {0};

static mst_ble_central_flag_t g_flags;
static uint8_t g_conn_role = 0;
static struct mst_hal_ble_mac g_target = {0};
static uint8_t g_data[250] = {0};

static uint16_t g_conn_hdl = 0xFFFF;
static uint16_t g_conn_num = 0;
static uint16_t g_at_count = 0;

static void mst_ble_central_init(void)
{
    ms_hal_ble_scan_stop();
    ms_hal_ble_conn_stop();
    if (ms_hal_ble_gap_connect_status(g_conn_hdl))
    {
        ms_hal_ble_gap_disconnect(g_conn_hdl);
        sleep(5);
    }

    g_flags.flags = 0x0000;

    memset(&g_target, 0, sizeof(g_target));
    memset(g_data, 0, sizeof(g_data));
    // 09:B6:12:73:5D:5B
    sscanf(aaaaddr, "%02x:%02x:%02x:%02x:%02x:%02x", &g_target.addr[0], &g_target.addr[1], &g_target.addr[2], &g_target.addr[3],
           &g_target.addr[4], &g_target.addr[5]);

    g_conn_hdl = 0xFFFF;
    g_conn_role = 0xFF;
    g_at_count = 0;

    memset(&g_service, 0, sizeof(g_service));
}

static int mst_hal_ble_stack_callback(ms_hal_ble_stack_msg_t event)
{
    uint16_t m_uuid = 0;
    MS_LOGD("ble stack callback: 0x%02x ", event.event_type);

    switch (event.event_type)
    {
    case MS_HAL_BLE_STACK_EVENT_CONNECTION_REPORT:
        MS_LOGD("%s:%d  %d\n", __FUNCTION__, __LINE__, event.param.connection_msg.conn_hdl);
        g_conn_hdl = event.param.connection_msg.conn_hdl;
        g_conn_role = event.param.connection_msg.role;
        g_flags.bits.conn = true;

        break;
    case MS_HAL_BLE_STACK_EVENT_DISC_SVC_REPORT:
        g_flags.bits.disc_service = true;
        m_uuid = event.param.disc_svc_msg.uuid[3] | (event.param.disc_svc_msg.uuid[2] << 8);
        MS_LOGD("%s:%d  %d  %x\n", __FUNCTION__, __LINE__, event.param.disc_svc_msg.uuid_len, m_uuid);
        if (0x1112 == m_uuid)
        {
            g_service.ff10_start_hdl = event.param.disc_svc_msg.start_hdl;
            g_service.ff10_end_hdl = event.param.disc_svc_msg.end_hdl;
        }

        break;

    case MS_HAL_BLE_STACK_EVENT_CMP_SVC_DISC:
        g_flags.bits.disc_service_cmp = true;

        break;
    case MS_HAL_BLE_STACK_EVENT_DISC_CHAR_REPORT:
        g_flags.bits.disc_char = true;
        m_uuid = event.param.disc_char_msg.uuid[3] | (event.param.disc_char_msg.uuid[2] << 8);
        MS_LOGD("%s:%d  %d  %x\n", __FUNCTION__, __LINE__, event.param.disc_svc_msg.uuid_len, m_uuid);
        if (0x2233 == m_uuid)
        {
            g_service.ff11_hdl = event.param.disc_char_msg.pointer_hdl;
            MS_LOGD("link [%d] ff11 handle: 0x%04x", event.param.disc_char_msg.conn_hdl, g_service.ff11_hdl);
        }
        else if (0x3334 == m_uuid)
        {
            g_service.ff12_hdl = event.param.disc_char_msg.pointer_hdl;
            MS_LOGD("link [%d] ff12 handle: 0x%04x", event.param.disc_char_msg.conn_hdl, g_service.ff12_hdl);
        }

        break;
    case MS_HAL_BLE_STACK_EVENT_CMP_CHAR_DISC:
        g_flags.bits.disc_char_cmp = true;

        break;
    case MS_HAL_BLE_STACK_EVENT_DISC_DESC_REPORT:
        g_flags.bits.disc_desc = true;
        m_uuid = event.param.disc_desc_msg.uuid[0] | (event.param.disc_desc_msg.uuid[1] << 8);
        MS_LOGD("%s:%d  %d  %x %x\n", __FUNCTION__, __LINE__, event.param.disc_svc_msg.uuid_len, event.param.disc_desc_msg.uuid[0],
                event.param.disc_desc_msg.uuid[1]);
        if (0x2902 == m_uuid)
        {
            g_service.ff12_cccd_hdl = event.param.disc_desc_msg.att_hdl;
        }

        break;
    case MS_HAL_BLE_STACK_EVENT_CMP_DISC_DESC_CHAR:
        g_flags.bits.disc_desc_cmp = true;
        break;
    case MS_HAL_BLE_STACK_EVENT_MTU_CHANGED:

        MS_LOGD("link [%d] mtu: %d - %d", g_conn_hdl, ms_hal_ble_mtu_get(g_conn_hdl), event.param.mtu_changed_msg.mtu);
        if (event.param.mtu_changed_msg.mtu == ms_hal_ble_mtu_get(g_conn_hdl))
        {
            g_flags.bits.mtu_cmp = true;
        }

        break;
    case MS_HAL_BLE_STACK_EVENT_CMP_WRITE_REQ:
        g_flags.bits.write_req = true;

        break;
    case MS_HAL_BLE_STACK_EVENT_CONNECT_PARAM_UPDATE_REQ:
        g_flags.bits.conn_param_update = true;
        MS_LOGD("MS_HAL_BLE_STACK_EVENT_CONNECT_PARAM_UPDATE_REQ");
        break;
    default:
        break;
    }
    return MS_HAL_RESULT_SUCCESS;
}

/**
 * @tc.number    : ms_hal_ble_conn_start_001
 * @tc.name      : call ms_hal_ble_conn_start start ble conn
 */
void ms_hal_ble_conn_start_001()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();

    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiConnStart1";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调

    // MS_BLE_TIMER(g_flags.bits.at_cmd, MS_TIMEOUT_MS_S(10));
    // if (!g_flags.bits.at_cmd) {
    //     MS_LOGD("%s can not recv at cmd", __FUNCTION__);
    //     TEST_FAIL();
    // }

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;

    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_SUCCESS);

    // MS_BLE_TIMER(g_flags.bits.conn, MS_TIMEOUT_MS_S(10));
    // MS_LOGD("%s g_flags.bits.conn 0x%x", __FUNCTION__, g_flags.bits.conn);
    // TEST_ASSERT_TRUE(g_flags.bits.conn);

    MS_LOGD("%s g_conn_role 0x%x", __FUNCTION__, g_conn_role);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(g_conn_role, 0);
};

/**
 * @tc.number    : ms_hal_ble_conn_start_002
 * @tc.name      : call ms_hal_ble_conn_start start ble conn
 */
void ms_hal_ble_conn_start_002()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiConnStart2";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调

    // MS_BLE_TIMER(g_flags.bits.at_cmd, MS_TIMEOUT_MS_S(10));
    // if (!g_flags.bits.at_cmd) {
    //     MS_LOGD("%s can not recv at cmd", __FUNCTION__);
    //     TEST_FAIL();
    // }

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_SUCCESS);

    // MS_BLE_TIMER(g_flags.bits.conn, MS_TIMEOUT_MS_S(10));
    // MS_LOGD("%s g_flags.bits.conn 0x%x", __FUNCTION__, g_flags.bits.conn);
    // TEST_ASSERT_TRUE(g_flags.bits.conn);

    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);
};

/**
 * @tc.number    : ms_hal_ble_conn_start_003
 * @tc.name      : call ms_hal_ble_conn_start start ble conn
 */
void ms_hal_ble_conn_start_003()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ret = ms_hal_ble_conn_start(&m_param, NULL);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);

    ms_hal_ble_set_stack_event_register(NULL);
};

/**
 * @tc.number    : ms_hal_ble_conn_start_004
 * @tc.name      : call ms_hal_ble_conn_start start ble conn
 */
void ms_hal_ble_conn_start_004()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;

    ret = ms_hal_ble_conn_start(NULL, &m_target);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);

    ms_hal_ble_set_stack_event_register(NULL);
};

/**
 * @tc.number    : ms_hal_ble_conn_stop_001
 * @tc.name      : call ms_hal_ble_conn_stop stop ble conn
 */
void ms_hal_ble_conn_stop_001()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);

    uint8_t m_tmp_target[6] = {0xA0, 0x94, 0x5D, 0x37, 0xB2, 0x30};

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, m_tmp_target, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    // MS_BLE_TIMER(g_flags.bits.conn, MS_TIMEOUT_MS_S(10));

    ret = ms_hal_ble_conn_stop();
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_SUCCESS);

    ms_hal_ble_set_stack_event_register(NULL);
};

/**
 * @tc.number    : ms_hal_ble_discovery_all_service_001
 * @tc.name      : call ms_hal_ble_discovery_all_service start discover all services of the device
 */
void ms_hal_ble_discovery_all_service_001()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiDiscService";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调

    // MS_BLE_TIMER(g_flags.bits.at_cmd, MS_TIMEOUT_MS_S(10));
    // if (!g_flags.bits.at_cmd) {
    //     MS_LOGD("%s can not recv at cmd", __FUNCTION__);
    //     TEST_FAIL();
    // }

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    // MS_BLE_TIMER(g_flags.bits.conn, MS_TIMEOUT_MS_S(5));
    // if (!g_flags.bits.conn) {
    //     MS_LOGD("%s g_flags.bits.conn 0x%x", __FUNCTION__, g_flags.bits.conn);
    //     TEST_FAIL();
    // }
    sleep(5);
    ret = ms_hal_ble_discovery_all_service(g_conn_hdl);

    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_SUCCESS);

    // MS_BLE_TIMER(g_flags.bits.disc_service, MS_TIMEOUT_MS_S(5));
    // if (!g_flags.bits.disc_service) {
    //     MS_LOGD("%s g_flags.bits.disc_service 0x%x", __FUNCTION__, g_flags.bits.disc_service);
    //     TEST_FAIL();
    // }

    // MS_BLE_TIMER(g_flags.bits.disc_service_cmp, MS_TIMEOUT_MS_S(5));

    // MS_LOGD("%s g_flags.bits.disc_service_cmp 0x%x", __FUNCTION__, g_flags.bits.disc_service_cmp);
    // TEST_ASSERT_TRUE(g_flags.bits.disc_service_cmp);
};

/**
 * @tc.number    : ms_hal_ble_discovery_all_service_002
 * @tc.name      : call ms_hal_ble_discovery_all_service start discover all services of the device
 */
void ms_hal_ble_discovery_all_service_002()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();

    ms_hal_result_t ret = ms_hal_ble_discovery_all_service(0xFFFF);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);
};

/**
 * @tc.number    : ms_hal_ble_discovery_all_char_001
 * @tc.name      : call ms_hal_ble_discovery_all_char start discover all characteristic of the device
 */
void ms_hal_ble_discovery_all_char_001()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiDiscChar";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调

    // MS_BLE_TIMER(g_flags.bits.at_cmd, MS_TIMEOUT_MS_S(10));
    // if (!g_flags.bits.at_cmd) {
    //     MS_LOGD("%s can not recv at cmd", __FUNCTION__);
    //     TEST_FAIL();
    // }

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);
    sleep(5);
    ret = ms_hal_ble_discovery_all_service(g_conn_hdl);
    sleep(5);
    ret = ms_hal_ble_discovery_all_char(g_conn_hdl, g_service.ff10_start_hdl, g_service.ff10_end_hdl);

    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_SUCCESS);

    // MS_BLE_TIMER(g_flags.bits.disc_char, MS_TIMEOUT_MS_S(5));
    // if (!g_flags.bits.disc_char) {
    //     MS_LOGD("%s g_flags.bits.disc_char 0x%x", __FUNCTION__, g_flags.bits.disc_char);
    //     TEST_FAIL();
    // }

    // MS_BLE_TIMER(g_flags.bits.disc_char_cmp, MS_TIMEOUT_MS_S(5));
    // MS_LOGD("%s g_flags.bits.disc_char_cmp 0x%x", __FUNCTION__, g_flags.bits.disc_char_cmp);
    // TEST_ASSERT_TRUE(g_flags.bits.disc_char_cmp);
};

/**
 * @tc.number    : ms_hal_ble_discovery_all_char_002
 * @tc.name      : call ms_hal_ble_discovery_all_char start discover all characteristic of the device
 */
void ms_hal_ble_discovery_all_char_002()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();

    ms_hal_result_t ret = ms_hal_ble_discovery_all_char(0xFFFF, 0, 0xFFFF);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);
};

/**
 * @tc.number    : ms_hal_ble_discovery_all_descriptor_001
 * @tc.name      : call ms_hal_ble_discovery_all_descriptor start discover all descriptor of the device
 */
void ms_hal_ble_discovery_all_descriptor_001()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiDiscDescriptor";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调

    // MS_BLE_TIMER(g_flags.bits.at_cmd, MS_TIMEOUT_MS_S(10));
    // if (!g_flags.bits.at_cmd) {
    //     MS_LOGD("%s can not recv at cmd", __FUNCTION__);
    //     TEST_FAIL();
    // }

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    ret = ms_hal_ble_discovery_all_service(g_conn_hdl);

    ret = ms_hal_ble_discovery_all_char(g_conn_hdl, g_service.ff10_start_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_discovery_all_descriptor(g_conn_hdl, g_service.ff12_hdl, g_service.ff10_end_hdl);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_SUCCESS);
};

/**
 * @tc.number    : ms_hal_ble_discovery_all_descriptor_002
 * @tc.name      : call ms_hal_ble_discovery_all_descriptor start discover all characteristic of the device
 */
void ms_hal_ble_discovery_all_descriptor_002()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();

    ms_hal_result_t ret = ms_hal_ble_discovery_all_descriptor(0xFFFF, 0, 0xFFFF);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);
};

/**
 * @tc.number    : ms_hal_ble_set_cccd_001
 * @tc.name      : call ms_hal_ble_set_cccd set cccd value
 */
void ms_hal_ble_set_cccd_001()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiSetCccd";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);
    sleep(5);
    ret = ms_hal_ble_discovery_all_service(g_conn_hdl);
    sleep(5);
    ret = ms_hal_ble_discovery_all_char(g_conn_hdl, g_service.ff10_start_hdl, g_service.ff10_end_hdl);
    sleep(5);
    ret = ms_hal_ble_discovery_all_descriptor(g_conn_hdl, g_service.ff12_hdl, g_service.ff10_end_hdl);
    sleep(5);
    ret = ms_hal_ble_set_cccd(0x0002, g_conn_hdl, g_service.ff12_cccd_hdl);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_SUCCESS);
};

/**
 * @tc.number    : ms_hal_ble_set_cccd_002
 * @tc.name      : call ms_hal_ble_set_cccd set cccd value
 */
void ms_hal_ble_set_cccd_002()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    sleep(5);
    ms_hal_result_t ret = ms_hal_ble_set_cccd(0x0002, 0xFFFF, 0xFFFF);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);
};

/**
 * @tc.number    : ms_hal_ble_set_cccd_003
 * @tc.name      : call ms_hal_ble_set_cccd set cccd value
 */
void ms_hal_ble_set_cccd_003()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiSetCccd";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    ret = ms_hal_ble_discovery_all_service(g_conn_hdl);

    ret = ms_hal_ble_discovery_all_char(g_conn_hdl, g_service.ff10_start_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_discovery_all_descriptor(g_conn_hdl, g_service.ff12_hdl, g_service.ff10_end_hdl);
    sleep(5);
    ret = ms_hal_ble_set_cccd(0x0002, g_conn_hdl, 0xFFFF);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);
};

/**
 * @tc.number    : ms_hal_ble_gatt_write_001
 * @tc.name      : call ms_hal_ble_gatt_write write data to peer device
 */
void ms_hal_ble_gatt_write_001()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiWrite";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);
    ret = ms_hal_ble_discovery_all_service(g_conn_hdl);

    ret = ms_hal_ble_discovery_all_char(g_conn_hdl, g_service.ff10_start_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_discovery_all_descriptor(g_conn_hdl, g_service.ff12_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_set_cccd(0x0002, g_conn_hdl, g_service.ff12_cccd_hdl);

    // ret = ms_hal_ble_exchange_mtu(g_conn_hdl, MS_BLE_DEFAULT_MTU);
    sleep(5);
    uint8_t m_data[250] = {0};
    uint8_t i = 0;
    for (i = 0; i < 250; i++)
    {
        m_data[i] = random() & 0xFF;
    }

    g_flags.bits.write_req = false;

    ret = ms_hal_ble_gatt_write(0, g_conn_hdl, g_service.ff11_hdl, 250, m_data);

    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_SUCCESS);
};

/**
 * @tc.number    : ms_hal_ble_gatt_write_002
 * @tc.name      : call ms_hal_ble_gatt_write write data to peer device
 */
void ms_hal_ble_gatt_write_002()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();

    ms_hal_result_t ret = MS_HAL_RESULT_ERROR;
    sleep(5);
    uint8_t m_data[250] = {0};
    uint8_t i = 0;
    for (i = 0; i < 250; i++)
    {
        m_data[i] = random() & 0xFF;
    }

    ret = ms_hal_ble_gatt_write(0, 0xFFFF, g_service.ff11_hdl, 250, m_data);
    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);
};

/**
 * @tc.number    : ms_hal_ble_gatt_write_003
 * @tc.name      : call ms_hal_ble_gatt_write write data to peer device
 */
void ms_hal_ble_gatt_write_003()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiWrite";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调
    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    ret = ms_hal_ble_discovery_all_service(g_conn_hdl);

    ret = ms_hal_ble_discovery_all_char(g_conn_hdl, g_service.ff10_start_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_discovery_all_descriptor(g_conn_hdl, g_service.ff12_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_set_cccd(0x0002, g_conn_hdl, g_service.ff12_cccd_hdl);

    // ret = ms_hal_ble_exchange_mtu(g_conn_hdl, MS_BLE_DEFAULT_MTU);
    sleep(5);
    g_flags.bits.write_req = false;

    uint8_t m_data[250] = {0};
    uint8_t i = 0;
    for (i = 0; i < 250; i++)
    {
        m_data[i] = random() & 0xFF;
    }

    ret = ms_hal_ble_gatt_write(0, g_conn_hdl, 0xFFFF, 250, m_data);

    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);
};

/**
 * @tc.number    : ms_hal_ble_gatt_write_004
 * @tc.name      : call ms_hal_ble_gatt_write write data to peer device
 */
void ms_hal_ble_gatt_write_004()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiWrite";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调
    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    ret = ms_hal_ble_discovery_all_service(g_conn_hdl);

    ret = ms_hal_ble_discovery_all_char(g_conn_hdl, g_service.ff10_start_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_discovery_all_descriptor(g_conn_hdl, g_service.ff12_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_set_cccd(0x0002, g_conn_hdl, g_service.ff12_cccd_hdl);

    // ret = ms_hal_ble_exchange_mtu(g_conn_hdl, MS_BLE_DEFAULT_MTU);

    g_flags.bits.write_req = false;
    sleep(5);
    uint8_t m_data[250] = {0};
    uint8_t i = 0;
    for (i = 0; i < 250; i++)
    {
        m_data[i] = random() & 0xFF;
    }

    ret = ms_hal_ble_gatt_write(0, g_conn_hdl, g_service.ff11_hdl, 0, m_data);

    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);
};

/**
 * @tc.number    : ms_hal_ble_gatt_write_005
 * @tc.name      : call ms_hal_ble_gatt_write write data to peer device
 */
void ms_hal_ble_gatt_write_005()
{
    MS_LOGD("%s", __FUNCTION__);
    mst_ble_central_init();
    ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
    char *p_adv = "msXtsApiWrite";
    //
    // AT+BCENTRALQ
    // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
    //注册at指令回调

    ms_hal_ble_conn_param_t m_param;
    m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
    m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
    m_param.slave_latency = MS_BLE_CONN_LETENCY;
    m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

    ms_hal_ble_addr_t m_target;
    m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
    memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
    ret = ms_hal_ble_conn_start(&m_param, &m_target);

    ret = ms_hal_ble_discovery_all_service(g_conn_hdl);

    ret = ms_hal_ble_discovery_all_char(g_conn_hdl, g_service.ff10_start_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_discovery_all_descriptor(g_conn_hdl, g_service.ff12_hdl, g_service.ff10_end_hdl);

    ret = ms_hal_ble_set_cccd(0x0002, g_conn_hdl, g_service.ff12_cccd_hdl);

    // ret = ms_hal_ble_exchange_mtu(g_conn_hdl, MS_BLE_DEFAULT_MTU);

    g_flags.bits.write_req = false;

    ret = ms_hal_ble_gatt_write(0, g_conn_hdl, g_service.ff11_hdl, 250, NULL);

    MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
    sleep(5);
    TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);

    ms_hal_ble_gap_disconnect(g_conn_hdl);
    ms_hal_ble_set_stack_event_register(NULL);
};

// /**
//  * @tc.number    : ms_hal_ble_send_param_update_cfm_001
//  * @tc.name      : call ms_hal_ble_send_param_update_cfm  comfirm connect param update
//  */
// void ms_hal_ble_send_param_update_cfm_001() {
//     MS_LOGD("%s", __FUNCTION__);
//     mst_ble_central_init();
//     ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);
//     char *p_adv = "msXtsApiConnParamUpdate";
//     //
//     // AT+BCENTRALQ
//     // mst_ble_bcentralq_send(1, AT_BLE_TLV_ADV_TPYE_CONN, p_adv, strlen(p_adv));
//     //注册at指令回调

//     ms_hal_ble_conn_param_t m_param;
//     m_param.conn_intv_min = MS_BLE_CONN_INTERVAL_MIN;
//     m_param.conn_intv_max = MS_BLE_CONN_INTERVAL_MAX;
//     m_param.slave_latency = MS_BLE_CONN_LETENCY;
//     m_param.timeout = MS_BLE_SUPERVISION_TIMEOUT;

//     ms_hal_ble_addr_t m_target;
//     m_target.addr_type = MS_HAL_BLE_ADDR_TYPE_RANDOM_STATIC;
//     memcpy(m_target.addr, g_target.addr, MS_BLE_MAC_LEN);
//     ret = ms_hal_ble_conn_start(&m_param, &m_target);

//     ret = ms_hal_ble_send_param_update_cfm(g_conn_hdl, true);

//     MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
//     sleep(5);TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_SUCCESS);
// };

// /**
//  * @tc.number    : ms_hal_ble_send_param_update_cfm_002
//  * @tc.name      : call ms_hal_ble_send_param_update_cfm  comfirm connect param update
//  */
// void ms_hal_ble_send_param_update_cfm_002() {
//     MS_LOGD("%s", __FUNCTION__);
//     mst_ble_central_init();
//     ms_hal_result_t ret = ms_hal_ble_set_stack_event_register(mst_hal_ble_stack_callback);

//     ret = ms_hal_ble_send_param_update_cfm(0xFFFF, true);

//     MS_LOGD("%s ret 0x%x", __FUNCTION__, ret);
//     sleep(5);TEST_ASSERT_EQUAL_UINT8(ret, MS_HAL_RESULT_ERROR);

//     ms_hal_ble_set_stack_event_register(NULL);
// };

int main_central_test(void)
{

    ms_hal_ble_stack_start();
    MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    ms_hal_ble_conn_start_001();
    sleep(5);
    ms_hal_ble_get_public_mac_address(NULL);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_conn_start_002();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_conn_start_003();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_conn_start_004();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_conn_stop_001();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_discovery_all_service_001();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_discovery_all_service_002();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_discovery_all_char_001();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_discovery_all_char_002();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_discovery_all_descriptor_001();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_discovery_all_descriptor_002();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_set_cccd_001();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_set_cccd_002();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_set_cccd_003();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_exchange_mtu_001();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_exchange_mtu_002();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_gatt_write_001();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_gatt_write_002();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_gatt_write_003();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_gatt_write_004();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_gatt_write_005();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_send_param_update_cfm_001();
    // sleep(5);
    // MS_LOGD("-----------------------------%s:%d\n", __FUNCTION__, __LINE__);
    // ms_hal_ble_send_param_update_cfm_002();
    // sleep(5);
}